<template>
  <div>
    <!-- 搜索条件盒子 -->
    <div>
      <el-row style="justify-content: space-between">
        <p class="pinformation" style="margin-top: 7px">搜索条件</p>
        <el-row style="padding-bottom: 5px">
          <el-button type="success" @click="handleQuery">搜索</el-button>
          <el-button class="business resetBtn" @click="resetQuery"
            >重置</el-button
          >
        </el-row>
      </el-row>
      <el-form label-width="70" ref="queryRef" :model="queryParams">
        <el-row>
          <el-col :span="12">
            <el-form-item label="方案编号：" prop="projectCode">
              <el-input
                maxlength="100"
                clearable
                placeholder="请输入方案编号"
                v-model="queryParams.projectCode"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="方案名称：" prop="projectName" label-width="80">
              <el-input
                maxlength="100"
                clearable
                placeholder="请输入方案编号"
                v-model="queryParams.projectName"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="委托单位：" prop="wtCustomName">
          <el-input
            maxlength="100"
            clearable
            placeholder="请输入委托单位"
            v-model="queryParams.wtCustomName"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始时间：">
              <el-date-picker
                v-model="dateRange"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="紧急类型：" prop="degree" label-width="80">
              <el-select
                v-model="queryParams.degree"
                placeholder="请选择紧急类型"
                clearable
                @change="handleQuery"
              >
                <el-option
                  v-for="dict in proxy.addListNoLimit(sys_degree)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 检测类别列表盒子 -->
    <div>
      <el-row>
        <p class="pinformation">方案列表</p>
        <p class="totalStyle">共 {{ total }} 条数据</p>
      </el-row>
      <el-table
        ref="mainTableRef"
        :data="checkTable"
        :row-class-name="tableRowClass"
        @row-click="rowClick"
        v-loading="loading"
        :row-style="rowStyle"
      >
      <el-table-column label="方案编号" align="center" prop="projectCode" width="130"/>
      <el-table-column label="受检单位" align="center" prop="sjCustomName" width="250"/>
      <el-table-column label="方案名称" align="center" prop="projectName" width="130"/>
      <el-table-column label="委托单位" align="center" prop="wtCustomName"  width="250"/>
      <el-table-column label="方案开始时间" align="center" prop="planDate" width="130"/>
      <el-table-column label="要求完成时间" align="center" prop="endTime" width="130"/>
      <el-table-column label="方案类型" align="center" width="200">
        <template #default="scope">
          <dict-tag :options="sys_contract_type" :value="scope.row.contractType" />
        </template>
      </el-table-column>
      <el-table-column label="采样员" align="center" prop="cyUserName" width="120" />
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script setup>
import { listProjct } from "@/api/sampleManage/sampleWith";

const { proxy } = getCurrentInstance();
const { sys_degree } = proxy.useDict(
  "sys_degree",
);
const {
  sys_contract_type,
} = proxy.useOrgDict(
  "sys_contract_type",
);
const total = ref(0);
const checkTable = ref([]);
const dateRange = ref([])
const loading = ref(true);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    degree: -1
  },
});

const { queryParams } = toRefs(data);

const props = defineProps({
  userStatus: {
    type: Number, //参数类型
  },
  userStepId: {
    type: Number
  }
});

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 74 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);
function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}
onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});

onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

const emits = defineEmits(["getSeqId"]);

function getList() {
  loading.value = true;
  queryParams.value.userStatus = props.userStatus
  queryParams.value.userStepId = props.userStepId
  listProjct(proxy.addDateRange(queryParams.value, dateRange.value)).then((res) => {
    checkTable.value = res.rows;
    total.value = res.total;
    loading.value = false;
  });
}

function rowClick(row) {
  emits("getSeqId", row.projectId);
  heightBright.value = row.projectId;
}

function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}
function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = []
  getList();
}

const heightBright = ref("");
function tableRowClass({ row }) {
  if (row) {
    if (row.projectId == heightBright.value) {
      return "current-row";
    }
    return "";
  }
}


function rowStyle({row}) {
    if (row.degree == 1) {
      return {'color':'red'}
    }
}
// defineExpose({
//   resetQuery
// });

getList();
</script>
